import { Component } from 'react';

import { Group, Boxs, Tab, Avatar, Button, ScrollList, Toast} from 'saltui';
const { HBox, Box } = Boxs;
import DB from '../../../app/db';
import { URLS,nodataIcon } from '../../../app/variables';
import CheckedBox from '../../../components/ddCheckboxField';

import './PageHome.less';

const Item1  =  React.createClass({

          detailsClick: function(event) {

              //location.hash='meeting/meetingDetails/'+this.props.noticeInfoId + '/notice';
           
          },
          render:function() {
               return <div> 
                <Group.List>
                <div>
                  <div className="dd-t-list-wrap-single">
                    <HBox vAlign="center">
                      <HBox flex={1}>
                        <Box>
                          <Avatar name={this.props.userName} className="t-MR6" />
                        </Box>
                        <Box className="dd-t-list-text-content-single" flex={1}>
                          <p className="dd-t-list-title-single omit t-LH40">{this.props.userName}</p>
                        </Box>
                      </HBox>
                    </HBox>
                  </div>
                </div>
              </Group.List></div>
          }
     });



export default class PageHome extends Component {

 constructor(props) {

    //关闭加载页的遮罩效果
    $(".loading-containter").hide();
    //禁用ios弹性事件
    dd.ui.webViewBounce.disable();
   var homeHeight = getScrollHigh(0, 1, 0);

    super(props);
    let id = this.props.params.id;
    let sign = this.props.params.sign;

    this.state = {
     meetingId:id,
     noresponseNum:'0',
     confirmNum:'0',
     leaveNum:'0',
     noSingnNum:'0',
     singnNum:'0',
     flag:sign,
     type: this.props.params.type,
     dingType: "1",
     myType: this.props.params.myType,
     signedDateTime: this.props.params.signedDateTime,
     isEmceeUser: this.props.params.isEmceeUser,
     isCreateUser: this.props.params.isCreateUser,
     userList: [],
     userIds: "",
     selectValue: [],
     currentTabType:0,
     homeHeight: homeHeight,
      isShowBtn: false
    };
    
    let t = this;
    let userId = document.getElementById("userHiddenId").value;
    var isHelpNew = 1;
    dd.util.domainStorage.getItem({ //帮助按钮
       name:'meeting_helpNew_'+userId , // 存储信息的key值
       onSuccess : function(info) {
            var helpNew = info.value;
            if( typeof(helpNew) == 'undefined' || helpNew == '' ){
              isHelpNew = 2;
            }
            //初始化按钮
            t.initButton(isHelpNew);
       },
       onFail : function(err) {
            //alert(JSON.stringify(err));
       }
    });
    
  }

  //初始化按钮
  initButton(isHelpNew){
    //设置帮助
    /*dd.biz.navigation.setIcon({
        showIcon : true,//是否显示icon
        iconIndex : isHelpNew,//显示的iconIndex,如上图
        onSuccess : function(result) {
            location.hash='help/help_index';
        },
        onFail : function(err) {
        //jsapi调用失败将会回调此函数
        }
    });*/
  }

  componentDidMount(){
    let t = this;
    var page_title = "参会详情";
    $("title").text(page_title);
    var thirdMenuType = getLoginUserInfo().thirdMenuType;
    if(thirdMenuType == 'DING'){
      dd.biz.navigation.setTitle({
        title : page_title,//控制标题文本，空字符串表示显示默认文本
        onSuccess : function(result) {},
        onFail : function(err) {}
      });
    }else if(thirdMenuType =='MH'){
      !!emp&&emp.window.setTitle({
        title: {
            text: page_title,
            icon: '',
            actionmenu: {}
        },
      });
    }

    if(this.state.type == '5'){
      var param = {
        id: this.state.meetingId,
        type: "end"
      };
      let t = this;
      DB.SomeModuleAPI.getAllApprovalUserIds(param).then((content) => {
        t.setState({
          userList: content.userList
        });
      }).catch((error) => {
        
      });
    }
  }

  handleChange(rows){
    this.setState({
    })
  }

  handleClickImg(event, imgUrl) {
    console.log(event);
    console.log(imgUrl);
  }

  handleDelete(event, dataItem) {
    console.log(event);
    console.log(dataItem);
  }
  handleClick(path){
    location.hash=path
  }


  // 请求参数
  beforeFetch(type,data,from){
    data.id = this.state.meetingId;
    data.type = type;
    
    return data;

  }

  // 首页返回数据参数
  processData(data){
    
    return data;
  }

  handleTabClick(type){
    var flag = this.state.flag;
    var dingType = "";
    if(flag == '0'){
      if(type == 2){
        dingType = "0";
      }else{
        dingType = "";
      }
    }else{
      if(type == 1){
        this.refs.meeting_noSignlist_ref.fetchData('top');
        dingType = "3";
      }else if(type == 3){
        dingType = "0";
      }else if(type == 0){
        this.refs.meeting_signlist_ref.fetchData('top');
      }else{
        dingType = "";
      }
    }
    this.setState({
      dingType: dingType,
      currentTabType: type
    });
  }

  handleDingClick(){
    var param = {
      id: this.state.meetingId,
      type: this.state.dingType
    };
    DB.SomeModuleAPI.getAllApprovalUserIds(param).then((content) => {
        var userArray = content.data;
        location.hash = 'meeting/sendRemind/' + this.state.meetingId + '/' + userArray;
       /* dd.biz.ding.create({
          users : userArray,// 用户列表，工号
          corpId: corpid, // 企业id
          type: 1, // 附件类型 1：image  2：link
          alertType: 2, // 钉发送方式 0:电话, 1:短信, 2:应用内
          alertDate: {"format":"yyyy-MM-dd HH:mm","value":"2015-05-09 08:00"},
          attachment: {
              images: [''],
          }, // 附件信息
          text: "",  // 正文
          bizType :0, // 业务类型 0：通知DING；1：任务；2：会议；
          onSuccess : function() {
              //onSuccess将在点击发送之后调用
          },
          onFail : function() {}
      });*/
    }).catch((error) => {
      //alert(JSON.stringify(error) + "22");
    });
  }

  updateSignedClick(){
    
  }

  handlePopClick(type,value){
    if(type == "0"){
      this.setState({
        userIds: "",
        selectValue: []
      });
    }else{
      let t = this;
      var userids = "";
      for(var i=0;i<value.length;i++){
        userids += value[i].value + ",";
      }
      if(userids != ""){
        userids = userids.substring(0,userids.length-1);
      }
      if(userids == ""){
        Toast.show({
        type: 'error',
          content: '请选择需添加的用户'
        });
        return;
      }
      t.setState({
        selectValue: value
      });
      let param = {signedUserIds:userids, applyRecordId:t.state.meetingId};
      DB.SomeModuleAPI.updateSignInfo(param).then((content) => {
        Toast.show({
        type: 'success',
          content: '添加到会人员成功'
        });

        var userList = t.state.userList;
        var userIds = userids.split(',');
        for(var i=0;i<userList.length;i++){
          for(var j=0;j<userIds.length;j++){
            if(userIds[j] == userList[i].value){
              userList.splice(i, 1);
            }
          }
        }
        t.setState({
          userList: userList,
          selectValue: null
        });

        var currentTabType = t.state.currentTabType;
        if(currentTabType == 0){
          t.refs.meeting_signlist_ref.fetchData('top');
        }else if(currentTabType == 1){
          t.refs.meeting_noSignlist_ref.fetchData('top');
        }
      }).catch((error) => {
        if(typeof(error.errorMsg) == 'undefined'){
          Toast.show({
          type: 'error',
          content: '添加到会人员失败'
           });
        }else{
          Toast.show({
          type: 'error',
          content: error.errorMsg
          });
        }
      });
    }
  }

  changeSelectUser(value){
    var ids = "";
    for(var i=0;i<value.length;i++){
      ids += value[i].value + ",";
    }
    if(ids != ""){
      ids = ids.substring(0,ids.length-1);
    }
    this.setState({
      userIds: ids,
      selectValue: value
    });
  }

  render() {
    
    const t = this;
   
    let butHtml = "";
    let nowDateTime = new Date().getTime();
    let signedDateTimelet = this.state.signedDateTime;
    if(signedDateTimelet != "empty"){
      while(signedDateTimelet.indexOf('-') >= 0){
       signedDateTimelet = signedDateTimelet.replace('-', '/');
      }
      signedDateTimelet = new Date(signedDateTimelet).getTime();
    }
    if(this.state.isShowBtn){
      if((this.state.myType=="myLaunch" || this.state.isEmceeUser=='1' || this.state.isCreateUser=='1') && (this.state.type=='1' || this.state.type=='4') && ((this.state.dingType=='3' && (nowDateTime > signedDateTimelet || signedDateTimelet=="0"))||this.state.dingType=='0')){
        butHtml = (
          <div className="t-bottom-fixed">
            <Button.Group>
              <Button type="primary" display="inline" onClick={() => {this.handleDingClick();}}>发送提醒</Button>
            </Button.Group>
          </div>
        );
      }
    }

    // var userList = this.state.userList;
    // if((this.state.myType=="myLaunch" || this.state.isEmceeUser=='1'|| this.state.isCreateUser=='1') && this.state.type=='5' && this.state.flag=='1' && (userList!=null && userList.length>0)){
    //   butHtml = (
    //     <div className="t-bottom-fixed" >
    //       <CheckedBox showType='1' label="添加到会人员" placeholder="请输入用户名称" data={userList}
    //         onChange={this.handlePopClick.bind(this, "1")} value={t.state.selectValue}/>
    //     </div>
    //   );
    // }

    let className = "page-list t-PB30";
    if(butHtml != ""){
      className = "page-list t-PB68";
    }
    let listHeight ={height: this.state.homeHeight};
    return (
      <div className="">
        <div className={this.state.flag=='0' ? '' :'t-DN'}>
           <Tab
              activeKey={'0'}
              onChange={(obj)=>{this.handleTabClick(obj.activeKey)}}
              showScroll
            >
            
              <Tab.Item title="确认参会">
                <div className="t-LH1_5">
                    <div style={listHeight}>
                      <ScrollList
                        className={className}
                        url={URLS.approvalNoticeInfoSetList}
                        dataType="json"
                        pageSize={10}
                        beforeFetch={(data,from) => {
                          data.id = this.state.meetingId;
                          data.type = "1";
                          return data;
                        }}
                        processData={(data) => data}
                        ref = 'meeting_confirmlist_ref'
                        noDataImage={nodataIcon}
                        currentPageKey="page"
                      >
                      <Item1 type="0" />
                      </ScrollList>
                   </div>
                </div>
              </Tab.Item>
           

            <Tab.Item title="请假">
              <div className="t-LH1_5">
                  <div  style={listHeight}>
                    <ScrollList
                      className={className}
                      url={URLS.approvalNoticeInfoSetList}
                      dataType="json"
                      pageSize={10}
                      beforeFetch={(data,from) => {
                        data.id = this.state.meetingId;
                        data.type = "2";
                        return data;
                      }}
                      processData={(data) => data}
                      ref = 'meeting_leavelist_ref'
                      noDataImage={nodataIcon}
                      currentPageKey="page"
                    >
                    <Item1 type="0" />
                    </ScrollList>
                 </div>
              </div>
            </Tab.Item>
            <Tab.Item title="未反馈">
              <div className="t-LH1_5">
                  <div  style={listHeight}>
                    <ScrollList
                      className={className}
                      url={URLS.approvalNoticeInfoSetList}
                      dataType="json"
                      pageSize={10}
                      beforeFetch={(data,from) => {
                        data.id = this.state.meetingId;
                        data.type = "0";
                        return data;
                      }}
                      processData={(data) => {
                        if(data.data==null || data.data==undefined || data.data.length==0){
                          t.setState({isShowBtn: false})
                        }else{
                          t.setState({isShowBtn: true})
                        }
                        return data;
                      }}
                      ref = 'meeting_noResponselist_ref'
                      noDataImage={nodataIcon}
                      currentPageKey="page"
                    >
                    <Item1 type="0" />
                    </ScrollList>
                 </div>
              </div>
            </Tab.Item>
          </Tab> 
           </div> 



           <div className={this.state.flag=='1' ? '' :'t-DN'}>
         <Tab
            activeKey={'0'}
             onChange={(obj)=>{this.handleTabClick(obj.activeKey)}}
            showScroll
          >

            <Tab.Item title="签到" data={{ customKey1: '自定义值1', customKey2: '自定义值2' }}>
              <div className="t-LH1_5">

                <div style={listHeight}>
                  <ScrollList
                    className={className}
                    url={URLS.approvalNoticeInfoSetList}
                    dataType="json"
                    pageSize={10}
                    beforeFetch={(data,from) => {
                      data.id = this.state.meetingId;
                      data.type = "4";
                      return data;
                    }}
                    processData={(data) => data}
                    ref = 'meeting_signlist_ref'
                    noDataImage={nodataIcon}
                    currentPageKey="page"
                  >
                  <Item1 type="0" />
                  </ScrollList>

               </div>
              
              </div>
            </Tab.Item>
            <Tab.Item title="未签到">
              <div className="t-LH1_5">
                  <div style={listHeight}>
                    <ScrollList
                      className={className}
                      url={URLS.approvalNoticeInfoSetList}
                      dataType="json"
                      pageSize={10}
                      beforeFetch={(data,from) => {
                        data.id = this.state.meetingId;
                        data.type = "3";
                        return data;
                      }}
                      processData={(data) => data}
                      ref = 'meeting_noSignlist_ref'
                      noDataImage={nodataIcon}
                      currentPageKey="page"
                    >
                    <Item1 type="0" />
                    </ScrollList>
                 </div>
              </div>
            </Tab.Item>
        

          <Tab.Item title="请假">
            <div className="t-LH1_5">
                <div style={listHeight}>
                  <ScrollList
                    className={className}
                    url={URLS.approvalNoticeInfoSetList}
                    dataType="json"
                    pageSize={10}
                    beforeFetch={(data,from) => {
                      data.id = this.state.meetingId;
                      data.type = "2";
                      return data;
                    }}
                    processData={this.processData.bind(this)}
                    ref = 'meeting_leavelist_ref'
                    noDataImage={nodataIcon}
                    currentPageKey="page"
                  >
                  <Item1 type="0" />
                  </ScrollList>
               </div>
            </div>
          </Tab.Item>
          <Tab.Item title="未反馈">
            <div className="t-LH1_5">
                <div style={listHeight}>
                  <ScrollList
                    className={className}
                    url={URLS.approvalNoticeInfoSetList}
                    dataType="json"
                    pageSize={10}
                    beforeFetch={(data,from) => {
                      data.id = this.state.meetingId;
                      data.type = "0";
                      return data;
                    }}
                    processData={(data) => {
                      if(data.data==null || data.data==undefined || data.data.length==0){
                        t.setState({isShowBtn: false})
                      }else{
                        t.setState({isShowBtn: true})
                      }
                      return data;
                    }}
                    ref = 'meeting_noResponselist_ref'
                    noDataImage={nodataIcon}
                    currentPageKey="page"
                  >
                  <Item1 type="0" />
                  </ScrollList>
               </div>
            </div>
          </Tab.Item>
        </Tab> 
        
         </div> 
         { butHtml }
      </div>)       
  }
}


